package me.firstapp.repository.shop.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import me.firstapp.module.shop.ProductComment;
import me.firstapp.repository.base.impl.EntityRepositoryImpl;
import me.firstapp.repository.shop.ProductCommentRepository;

@Repository
public class ProductCommentRepositoryImpl extends EntityRepositoryImpl<ProductComment>
		implements ProductCommentRepository {

	@Override
	protected Class<ProductComment> getPersistentClass() {
		return ProductComment.class;
	}

	@Override
	public List<ProductComment> findProductCommentList(Long productId, Long apiClientId) {
		StringBuilder sql = new StringBuilder(" FROM ProductComment bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<>();

		sql.append(" AND bean.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);
		
		sql.append(" AND bean.productId =:productId ");
		parameters.put("productId", productId);

		return super.query(ProductComment.class, sql.toString(), parameters);
	}

	@Override
	public ProductComment findByOrderNoAndMemberIdAndProductId(String orderNo, Long memberId, Long productId, Long apiClientId) {
		StringBuilder sql = new StringBuilder(" FROM ProductComment bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<>();

		sql.append(" AND bean.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);
		
		sql.append(" AND bean.orderNo =:orderNo ");
		parameters.put("orderNo", orderNo);
		
		sql.append(" AND bean.productId =:productId ");
		parameters.put("productId", productId);
		
		sql.append(" AND bean.fromMember.id =:memberId ");
		parameters.put("memberId", memberId);

		List<ProductComment> result = super.query(ProductComment.class, sql.toString(), parameters);
		return result.isEmpty() ? null : result.get(0);
	}

}
